package com.lagou.client.View;

import com.lagou.client.ClientIntiClose;
import com.lagou.client.ClientScanner;
import com.lagou.model.TestPaper;
import com.lagou.model.User;
import com.lagou.model.Message;

import java.io.*;
import java.util.function.Consumer;

/**
 * 管理员系统
 */
public class ClientManagerView {

    /**
     * 为了可以使用输入输出流，采用合成复用原则
     */
    private ClientIntiClose cic;

    public ClientManagerView (ClientIntiClose cic){
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    public void clientManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入管理员的账户信息：");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入管理员的密码信息:");
        String password = ClientScanner.getSc().next();
        Message message = new Message("managerCheck", new User(studentId,password));
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(message);
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("登陆成功，欢迎使用！");
            clientManagerMainPage();
        }else {
            System.out.println("用户名或密码错误！");
        }
    }


    /**
     * 管理员主界面
     */
    public void clientManagerMainPage() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println(" \n\n\t\t   在线考试系统");
            System.out.println("------------------------------------");
            System.out.println(" \n\t\t   管理员系统");
            System.out.println("   [1] 学员管理模块");
            System.out.println("   [2] 考题管理模块");
            System.out.println("   [0] 退出，返回上一页");
            System.out.println("------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    studentManagementModule();
                    break;
                case 2:
                    TestModule();
                    break;
                case 0:
                    return ;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }

    }

    /**
     * 学员管理模块
     */
    public void studentManagementModule() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println(" \n\n\t\t   在线考试系统");
            System.out.println("------------------------------------");
            System.out.println(" \n\t\t   管理员系统");
            System.out.println(" \n\t\t\t\t   学员管理模系统");
            System.out.println("   [1] 增加学员");
            System.out.println("   [2] 删除学员");
            System.out.println("   [3] 修改学员");
            System.out.println("   [4] 根据学号查找学员");
            System.out.println("   [5] 查找全部学员");
            System.out.println("   [0] 退出，返回上一页");
            System.out.println("------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    SaveStudent();
                    break;
                case 2:
                    removeStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    selectStudentByStudentId();
                    break;
                case 5:
                    selectAllStudent();
                    break;
                case 0:
                    return ;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }

    }

    /**
     * 新增学员信息
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void SaveStudent() throws IOException, ClassNotFoundException {
        System.out.println("请输入要新增学员的学号：");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入要新增学员的姓名：");
        String userName = ClientScanner.getSc().next();
        System.out.println("请输入要新增学员的密码信息:");
        String password = ClientScanner.getSc().next();
        Message message = new Message("SaveStudent", new User(studentId,userName,password));
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(message);
        System.out.println("客户端发送新增学员信息成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("学员账号信息增加成功！");
        }else {
            System.out.println("用户名已存在，请重新输入！");
        }
    }


    /**
     * 删除学员信息成功
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void removeStudent() throws IOException, ClassNotFoundException {
        System.out.println("请输入要删除学员的学号：");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入要删除学员的姓名：");
        String userName = ClientScanner.getSc().next();
        System.out.println("请输入要删除学员的密码信息:");
        String password = ClientScanner.getSc().next();
        Message message = new Message("removeStudent", new User(studentId,userName,password));
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(message);
        System.out.println("客户端发送删除学员信息成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("学员账号信息删除成功！");
        }else {
            System.out.println("用户名不已存在，请重新输入！");
        }
    }

    /**
     * 学员信息修改
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void updateStudent() throws IOException, ClassNotFoundException {
        System.out.println("请输入要修改学员的学号：");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入修改后的学员学号：");
        String updateStudentId = ClientScanner.getSc().next();
        System.out.println("请输入修改后的学员的姓名：");
        String userName = ClientScanner.getSc().next();
        System.out.println("请输入修改后的学员密码信息:");
        String password = ClientScanner.getSc().next();
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        Message message = message = new Message("updateStudent", new User(studentId,new User(updateStudentId,userName,password)));
        cic.getOos().writeObject(message);
        System.out.println("客户端发送修改学员信息成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("学员信息修改成功！");
        }else{
            System.out.println("用户名已存在，请重新输入！");
        }
    }

    /**
     * 根据学号查找学生
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void selectStudentByStudentId() throws IOException, ClassNotFoundException {
        System.out.println("请输入要查找的学员学号：");
        String studentId = ClientScanner.getSc().next();
        Message message = new Message("selectStudentByStudentId", new User(studentId));
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(message);
        System.out.println("客户端发送查找学员信息成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("学员账号信息为：姓名："+ message.getUser().getUserName()+",学号为:"+ message.getUser().getStudentId());
        }else {
            System.out.println("用户名不已存在，请重新输入！");
        }
    }

    /**
     *查询全部学生信息
     */
    public void selectAllStudent() throws IOException, ClassNotFoundException {
        Message message = new Message("selectAllStudent");
        cic.getOos().writeObject(message);
        System.out.println("客户端发送查找学员信息成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("学员信息为:");
            System.out.println("  姓名\t\t\t\t学号");
            int i = 1;
            for (User user:message.getUserList()){
                System.out.println(" "+i+"."+user.getUserName()+"\t\t\t\t"+user.getStudentId());
                i++;
            }
        }else{
            System.out.println("用户名已存在，请重新输入！");
        }
    }


    /**
     * 管理员考题模块
     */
    public void TestModule() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println(" \n\n\t\t   在线考试系统");
            System.out.println("------------------------------------");
            System.out.println(" \n\t\t   管理员系统");
            System.out.println(" \n\t\t\t\t   考题管理模系统");
            System.out.println("   [1] 增加考题");
            System.out.println("   [2] 删除考题");
            System.out.println("   [3] 修改考题");
            System.out.println("   [4] 根据题目查找考题");
            System.out.println("   [5] 查找全部考题");
            System.out.println("   [6] 导入考题");
            System.out.println("   [0] 退出，返回上一页");
            System.out.println("------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    saveExamQuestions();
                    break;
                case 2:
                    removeExamQuestions();
                    break;
                case 3:
                    updateExamQuestions();
                    break;
                case 4:
                    selectExamQuestionsByTopic();
                    break;
                case 5:
                    selectAllExamQuestions();
                    break;
                case 6:
                    importExamQuestions();
                    break;
                case 0:
                    return ;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }

    }


    /**
     * 增加考题
     */
    public void saveExamQuestions() throws IOException, ClassNotFoundException {
        System.out.println("请输入要新增考题的题目：");
        String topic = ClientScanner.getSc().next();
        System.out.println("请输入A选项：");
        String A = ClientScanner.getSc().next();
        System.out.println("请输入B选项：");
        String B = ClientScanner.getSc().next();
        System.out.println("请输入C选项：");
        String C = ClientScanner.getSc().next();
        System.out.println("请输入D选项：");
        String D = ClientScanner.getSc().next();
        System.out.println("请输入该题正确选项：");
        String correctAnswer = ClientScanner.getSc().next();
        Message message = new Message("saveExamQuestions",new TestPaper(topic,A,B,C,D,correctAnswer));
        cic.getOos().writeObject(message);
        System.out.println("客户端发送新增考题成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())){
            System.out.println("考题信息已添加成功：题目："+ message.getPaper().getTopic());
        }else {
            System.out.println("题目已存在，请重新输入！！！");
        }
    }

    /**
     * 删除考题
     *
     */
    public void removeExamQuestions() throws IOException, ClassNotFoundException {
        System.out.println("请输入要删除的考题题目：");
        String topic = ClientScanner.getSc().next();
        Message testPaperMessage = new Message("removeExamQuestions",new TestPaper(topic,null,null,null,null,null));
        cic.getOos().writeObject(testPaperMessage);
        System.out.println("客户端发送删除考题信息成功！");
        testPaperMessage = (Message) cic.getOis().readObject();
        if ("success".equals(testPaperMessage.getType())){
            System.out.println("考题信息已删除成功");
        }else {
            System.out.println("题目不存在，请重新输入！！！");
        }

    }

    /**
     * 修改考题
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void updateExamQuestions() throws IOException, ClassNotFoundException {
        System.out.println("请输入要修改的考题题目：");
        String topic = ClientScanner.getSc().next();
        System.out.println("请输入修改后的考题题目：");
        String newTopic = ClientScanner.getSc().next();
        System.out.println("请输入修改后的的考题A选项");
        String A = ClientScanner.getSc().next();
        System.out.println("请输入修改后的的考题B选项：");
        String B = ClientScanner.getSc().next();
        System.out.println("请输入修改后的的考题C选项：");
        String C = ClientScanner.getSc().next();
        System.out.println("请输入修改后的的考题D选项：");
        String D = ClientScanner.getSc().next();
        System.out.println("请输入修改后的的考题的正确选项：");
        String correctAnswer = ClientScanner.getSc().next();
        Message testPaperMessage = new Message("updateExamQuestions",new TestPaper(topic,new TestPaper(newTopic,A,B,C,D,correctAnswer)));
        cic.getOos().writeObject(testPaperMessage);
        System.out.println("客户端发送修改考题信息成功！");
        testPaperMessage = (Message) cic.getOis().readObject();
        if ("success".equals(testPaperMessage.getType())){
            System.out.println("考题信息修改成功！！");
        }else {
            System.out.println("题目不存在，请重新输入！！！");
        }
    }

    /**
     * 根具题目查询考题
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void selectExamQuestionsByTopic() throws IOException, ClassNotFoundException {
        System.out.println("请输入要查询的考题题目：");
        String topic = ClientScanner.getSc().next();
        Message testPaperMessage = new Message("selectExamQuestionsByTopic",new TestPaper(topic,null,null,null,null,null));
        cic.getOos().writeObject(testPaperMessage);
        System.out.println("客户端发送根据题目查询考题信息成功！");
        testPaperMessage = (Message) cic.getOis().readObject();
        if ("success".equals(testPaperMessage.getType())){
            System.out.println("考题信息查询成功，考题信息为"+testPaperMessage.getPaper());
        }else {
            System.out.println("题目不存在，请重新输入！！！");
        }
    }

    /**
     * 查询全部考题
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void selectAllExamQuestions() throws IOException, ClassNotFoundException {
        Message testPaperMessage = new Message("selectAllExamQuestions");
        cic.getOos().writeObject(testPaperMessage);
        System.out.println("客户端发送查询全部考题信息成功！");
        testPaperMessage = (Message)  cic.getOis().readObject();
        if ("success".equals(testPaperMessage.getType())){
            System.out.println("考题信息查询成功，考题信息为：");
            testPaperMessage.getListTestPaper().forEach(new Consumer<TestPaper>() {
                int i =  1;
                @Override
                public void accept(TestPaper testPaper) {
                    System.out.println(i+"."+testPaper.toString());
                    i++;
                }
            });


        }else {
            System.out.println("题目不存在，请重新输入！！！");
        }
    }

    public void importExamQuestions() throws IOException, ClassNotFoundException {
        System.out.println("请输入要导入考题的绝对路径:");
        String path = ClientScanner.getSc().next();
        if (null!=path){
                Message testPaperMessage = new Message("importExamQuestions",path);
                cic.getOos().writeObject(testPaperMessage);
                testPaperMessage = (Message) cic.getOis().readObject();
                if ("success".equals(testPaperMessage.getType())){
                    System.out.println("考题导入成功");

                }else {
                    System.out.println("题目不存在，请重新输入！！！");
                }
            }
    }


}
